﻿
writeOutContainer = function () {
    $("<div id='ShadowContainer' class='container' style='display: none;'></div>").appendTo("body");

}



writeConfirmDialog = function () {
    writeOutContainer();
    $(" <div class='outContainer' id='confirmContainer' style='display: none;'>  <table cellpadding='0' cellspacing='0' border='0' class='dialogWin'>   <tr>  <td class='titleClass'>   <div id='confirmTitle'> </div>"
                + "</td>   </tr>     <tr>   <td class='mainBody'>    <div class='txtMsgContainer' id='confirmMsg'> </div>   </td> </tr>   <tr>  <td class='mainBody' style='text-align: right; padding-right: 15px;'>     <img src='/_layouts/JM.JCY.Resource/images/confirm.png' style='cursor: hand; margin-right: 10px;'  id='dialogConfirm' /> <img src='/_layouts/JM.JCY.Resource/images/cancel.png' style='cursor: hand;' id='dialogCancel' />"
                + "</td>   </tr> <tr>  <td class='dialogBottomBorderStyle'>    </td>    </tr>   </table> </div>").appendTo("body");
}


writeAlertDialog = function () {
    writeOutContainer();
    $("<div class='outContainer' id='outContainer' style='display: none;'><table cellpadding='0' cellspacing='0' border='0' class='dialogWin'><tr><td class='titleClass'>"
                    + "<div id='alertTitle'></div></td></tr> <tr><td class='mainBody'><div class='txtMsgContainer' id='alertMsg'>    </div>   </td>   </tr>   <tr> <td class='mainBody' style='text-align: right; padding-right: 15px;'><img src='/_layouts/JM.JCY.Resource/images/button_ok.gif' style='cursor: hand;'"
                    + "onclick='closeAlertDialog();' />  </td>  </tr>   <tr>  <td class='dialogBottomBorderStyle'>          </td>    </tr>  </table></div>").appendTo("body");
}


showAlert = function (titleTxt, msgTxt, url) {

    if ($("#outContainer").html() == null) {
        writeAlertDialog();
    }


    titleTxt = '错误';

    var outContainer, container, title, msg, hiddenURL;

    var _outContainer = document.getElementById("outContainer");

    outContainer = $("#outContainer");
    container = $("#ShadowContainer");
    title = $("#alertTitle");
    msg = $("#alertMsg");

    $("#ShadowContainer").height($(document).height());
    var _scorllTop = document.documentElement.scrollTop;
    var _clientHeight = document.documentElement.clientHeight * 0.3;
    var dialogTop = _scorllTop + _clientHeight;
    $("#outContainer").css("top", dialogTop);

    outContainer.css("display", "block");
    container.css("display", "block");

    title.text(titleTxt);

    msg.text(msgTxt);

    drag(_outContainer);
}


showConfirm = function (titleTxt, msgTxt, okCallback, cancelCallback, yesIcon) {
    if ($("#confirmContainer").html() == null) {
        writeConfirmDialog();
    }

    titleTxt = 'Confirmation';
    var confirmContainer, container, title, msg, OKCtrl, cancelCtrl;
    confirmContainer = $("#confirmContainer");
    container = $("#ShadowContainer");
    title = $("#confirmTitle");
    msg = $("#confirmMsg");

    $("#ShadowContainer").height($(document).height());
    var _scorllTop = document.documentElement.scrollTop;
    var _clientHeight = document.documentElement.clientHeight * 0.3;
    var dialogTop = _scorllTop + _clientHeight;
    $("#outContainer").css("top", dialogTop);
    confirmContainer.css("display", "block");
    container.css("display", "block");
    title.text(titleTxt);
    msg.text(msgTxt);
    var _confirmContainer = document.getElementById("confirmContainer");

    drag(_confirmContainer);



    if (yesIcon == true) {
        $("#dialogConfirm").attr("src", "/_layouts/JM.JCY.Resource/images/yes.png");
        $("#dialogCancel").attr("src", "/_layouts/JM.JCY.Resource/images/no.png");
    }


    $("#dialogConfirm").click(function () {
        assureConfirmDialog();
        if (okCallback != undefined && okCallback != null) {
            okCallback();
            okCallback = null;
        }

        return true;
    });

    $("#dialogCancel").click(function () {
        closeConfirmDialog();
        if (cancelCallback != undefined && cancelCallback != null) {
            cancelCallback();
        }

        return false;
    });





}

function drag(o, s) {
    if (typeof o == "string") o = document.getElementById(o);
    o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
    o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
    o.orig_index = o.style.zIndex;

    o.onmousedown = function (a) {
        this.style.cursor = "move";
        this.style.zIndex = 10000;
        var d = document;
        if (!a) a = window.event;
        var x = a.clientX + d.body.scrollLeft - o.offsetLeft;
        var y = a.clientY + d.body.scrollTop - o.offsetTop;
        d.ondragstart = "return false;"
        d.onselectstart = "return false;"
        d.onselect = "document.selection.empty();"

        if (o.setCapture)
            o.setCapture();
        else if (window.captureEvents)
            window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);

        d.onmousemove = function (a) {
            if (!a) a = window.event;
            o.style.left = a.clientX + document.body.scrollLeft - x;
            o.style.top = a.clientY + document.body.scrollTop - y;
            o.orig_x = parseInt(o.style.left) - document.body.scrollLeft;
            o.orig_y = parseInt(o.style.top) - document.body.scrollTop;
        }

        d.onmouseup = function () {
            if (o.releaseCapture)
                o.releaseCapture();
            else if (window.captureEvents)
                window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
            d.onmousemove = null;
            d.onmouseup = null;
            d.ondragstart = null;
            d.onselectstart = null;
            d.onselect = null;
            o.style.cursor = "normal";
            o.style.zIndex = o.orig_index;
        }
    }

    if (s) {
        var orig_scroll = window.onscroll ? window.onscroll : function () { };
        window.onscroll = function () {
            orig_scroll();
            o.style.left = o.orig_x + document.body.scrollLeft;
            o.style.top = o.orig_y + document.body.scrollTop;
        }
    }
}


function assureConfirmDialog() {

    $("#ShadowContainer").css("display", "none");
    $("#confirmContainer").css("display", "none");

}

function closeConfirmDialog() {

    $("#ShadowContainer").css("display", "none");
    $("#confirmContainer").css("display", "none");


}

function closeAlertDialog() {

    $("#ShadowContainer").css("display", "none");
    $("#outContainer").css("display", "none");

}


function mouseOver(obj, overImgUrl) {
    img = obj.src;
    obj.src = overImgUrl;
}

function mouseOut(obj) {
    obj.src = img;
}